package com.example.test.controller;

import com.example.test.pojo.Student;
import com.example.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.view.RedirectView;

/**
 * @author Cavan
 * @date 2023-06-15
 * @qq 2069543852
 */
@Controller
public class SystemController {


    @Autowired
    private StudentService studentService;


    //登录功能
    @PostMapping("/loginOut")
    public RedirectView login(String id, String password, Model model) {
        // 验证账号密码的逻辑
        Student student = studentService.getStuById(id);
        if (!ObjectUtils.isEmpty(student)){
            if (student.getPassword().equals(password)){
                return new RedirectView("/index");
            }else {
                // 登录失败，返回登录页面
                model.addAttribute("error", "账号或密码错误");
                return new RedirectView("/login");
            }
        }else {
            // 登录失败，返回登录页面
            model.addAttribute("error", "账号或密码错误");
            return new RedirectView("/login");
        }
    }

}
